from math import sin, asin, cos, radians, sqrt

# 更新覆盖文件
def update_file(file_name, file_content):
    """
        更新文件
    """
    with open(file_name, 'w', encoding="utf-8") as f:
        f.write(file_content)

def calculate_acceleration(initial_velocity, final_velocity, displacement):
    """
        计算加速度
    """
    acceleration = (final_velocity ** 2 - initial_velocity ** 2) / (2 * displacement)
    return acceleration

# 判断两个点经纬度距离单位 m
def calculate_distance(lon, lat, lon2, lat2):
    """
        计算两个点经纬度距离单位 m
    """
    lng1, lat1, lng2, lat2 = map(radians, [lon, lat, lon2, lat2])  # 经纬度转换成弧度
    dlon = lng2 - lng1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    distance = 2 * asin(sqrt(a)) * 6371.393 * 1000  # 地球平均半径，6371.393km
    return distance

def calc_time_to_reach(target_speed, target_distance, current_speed, current_acceleration):
    """
        计算到达目标速度所需时间
    """
    v = float(target_speed)
    d = float(target_distance)
    v0 = float(current_speed)
    a = float(current_acceleration)
    t = (v - v0) / a + (d - (v ** 2 - v0 ** 2) / (2 * a)) / v
    return t